package org.emergency.framework.dao.oa.Impl;

import java.util.List;

import org.emergency.framework.dao.HibernateBaseDao;
import org.emergency.framework.dao.oa.ITbEntityDAO;
import org.emergency.framework.model.TbEntity;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author Administrator
 *
 */
@Repository("tbEntityDAO")
public class TbEntityDAO extends HibernateBaseDao<TbEntity> implements ITbEntityDAO {
	private static final Logger log = LoggerFactory
			.getLogger(TbEntityDAO.class);

	protected void initDao() {
		// do nothing
	}

	public TbEntity findById(java.lang.String id) {
		log.debug("getting TbEntity instance with id: " + id);
		try {
			TbEntity instance = (TbEntity) getHibernateTemplate().get(
					"org.dao.TbEntity", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<TbEntity> findByExample(TbEntity instance) {
		log.debug("finding TbEntity instance by example");
		try {
			List<TbEntity> results = (List<TbEntity>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TbEntity instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TbEntity as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all TbEntity instances");
		try {
			String queryString = "from TbEntity";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TbEntity merge(TbEntity detachedInstance) {
		log.debug("merging TbEntity instance");
		try {
			TbEntity result = (TbEntity) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TbEntity instance) {
		log.debug("attaching dirty TbEntity instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TbEntity instance) {
		log.debug("attaching clean TbEntity instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TbEntityDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TbEntityDAO) ctx.getBean("TbEntityDAO");
	}
}